package com.itheima.factory;

import com.itheima.controller.IStudentController;
import com.itheima.controller.impl.StudentControllerImpl;
import com.itheima.dao.IStudentDao;
import com.itheima.dao.impl.StudentDaoImpl;
import com.itheima.service.IStudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Properties;

/*
工厂类
用来生产需要的对象

 */
public class Factory {
    private static final Logger log= LoggerFactory.getLogger(Factory.class);

    private static Properties prop = new Properties();
    //工厂的核心
    public static HashMap<String, Object> map = new HashMap<>();

    static {

        try {
            prop.load(ClassLoader.getSystemResourceAsStream("ObjectImpl.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*    FileInputStream is = new FileInputStream("\\zhongjixiangmu\\resource\\ObjectImpl.properties");
            prop.load(is);*/
            //顺序 StudentController
            //StudentService
            //TeacherDao
            //StudentDao
            //TeacherService
            //TeacherController

          prop.forEach((k,v)->{
              try {
                 // String className = prop.getProperty(k.toString());
                  //使用反射获取类的字节码对象
                  Class<?> clazz = Class.forName(v.toString());
                  Object o = createObject(clazz);
                  //将创建好的对象 设置到容器中
                  map.put(k.toString(), o);
              } catch (Exception e) {
                  e.printStackTrace();
                  log.error("读取创建对象的配置文件出错了"+e.getMessage());
              }

          });



    }

    private static Object createObject(Class<?> clazz) throws Exception {
        //通过字节码对象获取空参构造方法
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        //暴力反射
        constructor.setAccessible(true);
        //使用空参构造创建对象
        return constructor.newInstance();
    }



    /*
    生产学生用户操作类对象
     */
    public static IStudentController getStudentController()  {

        //从容器中获取已经创建好的对象
        try {
            if(map.get("IStudentController")==null){
                String className = prop.getProperty("IStudentController");
                //使用反射获取类的字节码对象
                Class<?> clazz = Class.forName(className);
                Object o=createObject(clazz);
                //将创建好的对象 设置到容器中
                map.put("IStudentController", o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  (IStudentController) map.get("IStudentController") ;
    }

    public static IStudentService getStudentService() {
       // String service = prop.getProperty("IStudentService");
        try {
            if(map.get("IStudentService")==null){
                String className = prop.getProperty("IStudentService");
                //使用反射获取类的字节码对象
                Class<?> clazz = Class.forName(className);
                Object o=createObject(clazz);
                //将创建好的对象 设置到容器中
                map.put("IStudentService", o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (IStudentService) map.get("IStudentService");
    }

    public static IStudentDao getStudentDao()  {
        //String dao = prop.getProperty("IStudentDao");
        try {
            if(map.get("IStudentDao")==null){
                String className = prop.getProperty("IStudentDao");
                //使用反射获取类的字节码对象
                Class<?> clazz = Class.forName(className);
                Object o=createObject(clazz);
                //将创建好的对象 设置到容器中
                map.put("IStudentDao", o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (IStudentDao) map.get("IStudentDao");
    }


}
